import json
import bottle
import Controller
import datetime, bson

config_file = open('config.json')
config = json.load( config_file )

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime): return str(obj)
        if isinstance(obj, bson.objectid.ObjectId): return str(obj)
        return json.JSONEncoder.default(self, obj)

controller = Controller.Controller();

@bottle.route('/recipes/')
def recipes_list():
	controller.list("Recipes")

@bottle.route('/recipes/<field>/<value>', method='GET')
def recipe_show(field="", value=""):
	return json.dumps(controller.find("Recipes", field, value), cls = MyEncoder)

@bottle.route('/recipes/', method='DELETE')
def recipe_delete():
	controller.delete("Recipes")

@bottle.route('/recipes/', method='PUT')
def recipe_save():
	controller.save("Recipes")

bottle.debug(mode=True)
bottle.run(host=config['http']['hostname'], port=config['http']['port'])